package ch.bfh.abhmcm.SoftwareEngineeringAndDesign.view.mainframe.mainframeSub.menubar;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.view.mainframe.Mainframe;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.view.mainframe.mainframeSub.configfile.ConfigFileHandler;

public class MenuOption extends JMenu {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Mainframe mainframe;

	public MenuOption(Mainframe mainframe, String text) {
		super(text);

		this.mainframe = mainframe;

		this.add(new LaF("Look and Feel anpassen"));
	}

	private class LaF extends JMenu {

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		private LaF(String text) {
			super(text);
			this.add(new LookandFeelOption("Nimbus Look&Feel",
					"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"));
			this.add(new LookandFeelOption("System Look&Feel", UIManager
					.getSystemLookAndFeelClassName()));
			this.add(new LookandFeelOption("Motif Look&Feel",
					"com.sun.java.swing.plaf.motif.MotifLookAndFeel"));
			this.add(new LookandFeelOption("Windows Look&Feel",
					"com.sun.java.swing.plaf.windows.WindowsLookAndFeel"));
			this.add(new LookandFeelOption("Tatoo Look&Feel",
					"com.jtattoo.plaf.mcwin.McWinLookAndFeel"));
			this.add(new LookandFeelOption("NimROD Look&Feel",
					"com.nilo.plaf.nimrod.NimRODLookAndFeel"));
		}
	}

	private class LookandFeelOption extends JMenuItem {

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		private LookandFeelOption(String text, String laF) {
			super(text);
			this.addActionListener(new LookandFeelChangeListener(laF));
		}

	}

	private class LookandFeelChangeListener implements ActionListener {

		private String laF;

		private LookandFeelChangeListener(String laF) {
			this.laF = laF;
		}

		@Override
		public void actionPerformed(ActionEvent e) {

			try {
				mainframe.setVisible(false);
				UIManager.setLookAndFeel(this.laF);
				SwingUtilities.updateComponentTreeUI(mainframe);
				new ConfigFileHandler().writeFirstLine(laF);
			} catch (Exception e1) {
				System.out.println(e1);
			}

			mainframe.setVisible(true);
		}

	}

}
